package ch09.game;

import java.io.*;

//
public class DataStreamManager {

    public static void save(int[] position){
        try(DataOutputStream dos = new DataOutputStream(new FileOutputStream("data/game1.save"))) {
            int x = position[0];
            int y = position[1];
            dos.writeInt(x);
            dos.writeInt(y);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static int[] load(){
        System.out.println("DataStreamManager.....");
        int[] position = {0,0};
        try(DataInputStream dis = new DataInputStream(new FileInputStream("data/game1.save"))){
            int x = dis.readInt();
            int y = dis.readInt();
            position[0] = x;
            position[1] = y;
        } catch (FileNotFoundException e) {
//            e.printStackTrace();
            System.out.println("没有找到记录文件，使用默认数据");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return position;
    }
}
